﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Class_Entity;
using BussinessObject;
using System.Data;
using System.Data.SqlClient;

public partial class WebApplication_ADAddTablet : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }


    protected void btSubmit_Click(object sender, EventArgs e)
    {
        Tablet ta = new Tablet();
        BOProducts bo = new BOProducts();

        // fill information into object ta

        ta.Model = txtModel.Text;
        ta.Dies = txtDies.Text;
        ta.MaxDiameterOfTablet =Convert.ToInt32(txtDia.Text);
        ta.MaxPressure = Convert.ToInt32(txtMaxP.Text);
        ta.MaxDepthOfFill = Convert.ToInt32(txtMaxDep.Text);
        ta.ProductionCapacity = txtCap.Text;
        ta.Size = txtSize.Text;
        ta.Weight = txtWeight.Text;

        
        ta.Image = "~\\WebApplication\\images\\" + FileUploadImage.FileName;
        

        FileUploadImage.SaveAs(Server.MapPath("~\\WebApplication\\images\\" + FileUploadImage.FileName));

        // check if validation are true then add to database

        if (Session["a"] == null)
        {
            bo.Add_Tablet(ta);
            HttpContext.Current.Response.Write("<script type='text/javascript'>alert('" + "Add Successfully ..." + "');window.location.href='ADTablet.aspx';</script>");


        }
    }


    protected void btReset_Click(object sender, EventArgs e)
    {
        resetAll();
    }

    private void resetAll()
    {
        txtModel.Text = "";
        txtDies.Text = "";
        txtCap.Text = "";
        txtDia.Text = "";
        txtMaxDep.Text = "";
        txtMaxP.Text = "";
        txtSize.Text = "";
        txtWeight.Text = "";

    }

    // check if model name exits or not

    protected void txtModel_TextChanged(object sender, EventArgs e)
    {
        Tablet ta = new Tablet();
        BOProducts bo = new BOProducts();
        DataSet ds = bo.Load_Tablet(ta);
        DataTable dt = ds.Tables[0];
        Label1.Text = "";
        Session["a"] = null;

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (txtModel.Text.Trim() == dt.Rows[i]["Model"].ToString())
            {
                Session["a"] = 1;
                Label1.Text = "Model already exits";
            }

        }
    }
}